package com.lagou.rabbitmq.controller;

import com.lagou.rabbitmq.entity.OrderInfo;
import com.lagou.rabbitmq.service.OrderService;
import com.lagou.rabbitmq.util.SimpleResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/order")
    public ResponseEntity order(@RequestParam @NotNull(message = "用户ID不能为空") Long userId,
                                @RequestParam @NotNull(message = "课程ID不能为空") Integer courseId) throws Exception {
        Long orderId = orderService.order(userId, courseId);
        return SimpleResponseEntity.post(orderId);
    }

    @GetMapping("/order/{orderId}")
    public ResponseEntity<OrderInfo> getOrderInfoById(@RequestParam @NotNull(message = "用户ID不能为空") Long userId,
                                                      @PathVariable @NotNull(message = "订单ID不能为空") Long orderId) throws Exception {
        OrderInfo orderInfo = orderService.getOrderInfoById(userId, orderId);
        return SimpleResponseEntity.get(orderInfo);
    }
}
